package com.myhexin.demo.ignite.test.base;

import org.apache.ignite.Ignition;
import org.apache.ignite.client.ClientCache;
import org.apache.ignite.client.ClientTransaction;
import org.apache.ignite.client.ClientTransactions;
import org.apache.ignite.client.IgniteClient;
import org.apache.ignite.configuration.ClientConfiguration;
import org.apache.ignite.configuration.ClientTransactionConfiguration;
import org.apache.ignite.transactions.TransactionConcurrency;
import org.apache.ignite.transactions.TransactionIsolation;

/**
 * @author zhangzhonglv@myhexin.com
 * @date 2025/8/15
 */
public class TransactionDemo {

    private static final String IGNITE_ADDR = "127.0.0.1:10800";

    public static void main(String[] args) {
        ClientConfiguration cfg = new ClientConfiguration();
        cfg.setAddresses(IGNITE_ADDR);
        cfg.setPartitionAwarenessEnabled(true);
        cfg.setTransactionConfiguration(new ClientTransactionConfiguration().setDefaultTxTimeout(10000)
                .setDefaultTxConcurrency(TransactionConcurrency.OPTIMISTIC)
                .setDefaultTxIsolation(TransactionIsolation.REPEATABLE_READ));

        try (IgniteClient client = Ignition.startClient(cfg)) {
            ClientCache<Integer, String> cache = client.getOrCreateCache("myCache");
            ClientTransactions tx = client.transactions();
            try (ClientTransaction t = tx.txStart()) {
                cache.put(1, "new value");
                t.commit();
            }
        }
    }
}
